#include "udp_server.hpp"
#include <memory>

using namespace ns_server;

void usage()
{
    std::cout << "usage:\n\t" << "./udp_server port\n" << std::endl; 
}

std::string ExcuteCommend(std::string command)
{
    // 该接口可以将传入的指令（字符串），直接调用，并通过文件的方式返回结果
    FILE* fp = popen(command.c_str(), "r"); 
    if(fp == nullptr)
    {
        return "Wrong Command!";
    }

    char line[1024];
    std::string result;
    while(fgets(line, sizeof(line), fp) != NULL)
    {
        result += line;
    }
    pclose(fp);
    return result;
}

int main(int argc, char* argv[])
{
    if(argc != 2)
    {
        usage();
        exit(USAGE_ERR);
    }
    uint16_t port = atoi(argv[1]);
    // std::unique_ptr<udp_server> server(new udp_server(ExcuteCommend, port));
    std::unique_ptr<udp_server> server(new udp_server(port));

    // server->InitServer();
    // server->Start();
    server->Start();
    
    return 0;
}